﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using ExamAnalysis.Core;
using ExamAnalysis.Model;
using ExamAnalysis.Core.Statistics;
using System.Xml;
using System.Xml.Xsl;
using System.Web.Security;

namespace ExamAnalysis.Reports
{
    public partial class MicroReportbyACL : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    this.CalendarStore.DataSource = db.SchoolCalender
                        .Where(p => p.SchoolID == Config.CurrentSchoolID)
                        .OrderByDescending(p => p.CalendarID);
                    this.CalendarStore.DataBind();
                    BindExamList();
                }
                if (Request.QueryString["type"] == "exam")
                {
                    using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                    {
                        CalendarList.Text = (from c in db.ExamInfo
                                             where c.ExamID == Convert.ToInt32(Request.QueryString["id"])
                                             select c.SchoolCalender.DisplayName).First().ToString();
                        ExamList.Text = (from ex in db.ExamInfo
                                         where ex.ExamID == Convert.ToInt32(Request.QueryString["id"])
                                         select ex.ExamName).First().ToString();
                        List<ExamInfo> ei = null;
                        if (Request.QueryString["role"] == null)
                        {
                            ei = RoleData.GetExamInfoByUserName(Membership.GetUser().UserName);
                        }
                        else if (Roles.GetRolesForUser().Contains(Request.QueryString["role"]))
                            ei = RoleData.GetExamInfoOfRole(Membership.GetUser().UserName, Request.QueryString["role"]);
                        ExamStore.DataSource = ei
                            .Where(p => p.ExamID == Convert.ToInt32(Request.QueryString["id"]))
                            .OrderByDescending(p => p.ExamID);
                        ExamStore.DataBind();
                    }
                }
            }
        }
        public void CanlendarList_SelectedItemChanged(object sender, DirectEventArgs e)
        {
            BindExamList();
        }
        protected void BindExamList()
        {
            if (CalendarList.SelectedItem != null)
            {
                int CalendarID = Convert.ToInt32(CalendarList.SelectedItem.Value);
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    List<ExamInfo> ei = null;
                    if (Request.QueryString["role"] == null)
                    {
                        ei = RoleData.GetExamInfoByUserName(Membership.GetUser().UserName);
                    }
                    else if (Roles.GetRolesForUser().Contains(Request.QueryString["role"]))
                        ei = RoleData.GetExamInfoOfRole(Membership.GetUser().UserName, Request.QueryString["role"]);
                    if (Request.QueryString["type"] != "exam")
                    {
                        ExamStore.DataSource = ei
                            .Where(p => p.CalendarID == CalendarID)
                            .OrderByDescending(p => p.ExamID);
                    }
                    else
                    {
                        ExamStore.DataSource = ei
                            .Where(p => p.CalendarID == CalendarID && p.ExamID == Convert.ToInt32(Request.QueryString["id"]))
                            .OrderByDescending(p => p.ExamID);
                    }
                    ExamStore.DataBind();
                }
            }
        }
        protected void SubmitButton_Click(object sender, EventArgs e)
        {
            using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
            {
                if (ExamList.SelectedItem == null)
                {
                    X.Msg.Alert("通知", "请选择相应考试！").Show();
                }
                else
                {
                    GridStore.DataSource = Report.ProcessMicroReport(Convert.ToInt32(ExamList.SelectedItem.Value.ToString()));
                    GridStore.DataBind();
                }
            }
        }
        protected void Store_Submit(object sender, StoreSubmitDataEventArgs e)
        {
            string format = this.FormatType.Value.ToString();

            XmlNode xml = e.Xml;

            this.Response.Clear();

            switch (format)
            {
                case "xml":
                    string strXml = xml.OuterXml;
                    this.Response.AddHeader("Content-Disposition", "attachment; filename=submittedData.xml");
                    this.Response.AddHeader("Content-Length", strXml.Length.ToString());
                    this.Response.ContentType = "application/xml";
                    this.Response.Write(strXml);
                    break;

                case "xls":
                    this.Response.ContentType = "application/vnd.ms-excel";
                    this.Response.AddHeader("Content-Disposition", "attachment; filename=submittedData.xls");
                    XslCompiledTransform xtExcel = new XslCompiledTransform();
                    xtExcel.Load(Server.MapPath("~/Reports/Export/Excel.xsl"));
                    xtExcel.Transform(xml, null, Response.OutputStream);

                    break;

                case "csv":
                    this.Response.ContentType = "application/octet-stream";
                    this.Response.AddHeader("Content-Disposition", "attachment; filename=submittedData.csv");
                    XslCompiledTransform xtCsv = new XslCompiledTransform();
                    xtCsv.Load(Server.MapPath("~/Reports/Export/Csv.xsl"));
                    xtCsv.Transform(xml, null, Response.OutputStream);

                    break;
            }
            this.Response.End();
        }
    }
}